package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;


//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {

    @Override
    public ItemType convert(String code) {

//        ItemType.class.getEnumConstants(); // 同样可以拿到这个枚举类型的实例对象
        ItemType[] itemTypes = ItemType.values();
        for (ItemType itemType : itemTypes) {
            if (itemType.getCode().equals(Integer.valueOf(code))) {
                return itemType;
            }
        }
        throw new IllegalArgumentException("code" + code + "非法");
    }
}
